<?php

namespace App\Models\Traits;

use Carbon\Carbon;

trait HasUserWithdraw
{
	public function hasAppliedWithdrawToday()
	{
		return $this->withdraws()
			->whereDate('created_at', Carbon::today())
			->exists();
	}

	public function hasWithdrewToday()
	{
		return $this->withdraws()
			->whereDate('created_at', Carbon::today())
			->whereNotNull('paid_at')->exists();
	}

	public function latestWithdrawRecord()
	{

		$latestWithdrawRecord =  $this->withdraws()
									->where('status','paid')
									->where('created_at', '>', '2022-05-31 23:50:00')
									->orderByDesc("created_at")->first();
		if(!$latestWithdrawRecord){
			$latestWithdrawRecord =  $this->withdraws()
									->where('status', '!=', 'failed')
									->where('status', '!=', 'returned')
									->where('method','sandpay')
									->orderByDesc("created_at")->first();
		}
		return $latestWithdrawRecord;
	}

	public function latestWithdrawRecordNotInitTime()
	{
		$latestWithdrawRecord =  $this->withdraws()
									->where('status','paid')
									->orderByDesc("created_at")->first();
		return $latestWithdrawRecord;
	}

	public function initFreeWithdrwaQuota()
	{
		$userCharges = $this->goldChargeTotalAmount();
		$realUseMoney= big_decimal($userCharges)
						->plus($this->transferedInCoins())
						->plus($this->transferedOutCoins())
						->plus($this->payOrderCoins());

		if($realUseMoney->plus($this->goldRecordTotal())->isLessThanOrEqualTo(0)){
			$useMoney =  $realUseMoney;
		}else{
			$useMoney = big_decimal(abs($this->goldRecordTotal()));
		}

		$freeWithdrawQuota = $useMoney->plus($this->netProfitTotal())->plus($this->goldBeansTotal());

		if($freeWithdrawQuota->isLessThan(0)){
			$freeWithdrawQuota = big_decimal(0);
		}
		return $freeWithdrawQuota;
	}


	public function freeWithdrawQuota()
	{
		if(empty($this->latestWithdrawRecord()) && $this->pre_free_withdraw_quota <= 0){
			$freeWithdrawQuota = $this->initFreeWithdrwaQuota();
		}elseif($this->pre_free_withdraw_quota > 0 && empty($this->latestWithdrawRecord())){
			if(empty($this->latestWithdrawRecordNotInitTime())){
				$freeWithdrawQuota = big_decimal($this->pre_free_withdraw_quota);

			}else{
				$freeWithdrawQuota = big_decimal($this->pre_free_withdraw_quota)
								->plus(abs($this->preGoldRecordTotal($this->latestWithdrawRecordNotInitTime()->created_at)))
								->plus($this->preNetProfitTotal($this->latestWithdrawRecordNotInitTime()->created_at))
								->plus($this->preGoldBeansTotal($this->latestWithdrawRecordNotInitTime()->created_at));
			}
		}else{
			if(empty($this->latestWithdrawRecord())){
				$freeWithdrawQuota = big_decimal($this->pre_free_withdraw_quota);
			}else{
				$freeWithdrawQuota = big_decimal($this->pre_free_withdraw_quota)
								->plus(abs($this->preGoldRecordTotal($this->latestWithdrawRecord()->created_at)))
								->plus($this->preNetProfitTotal($this->latestWithdrawRecord()->created_at))
								->plus($this->preGoldBeansTotal($this->latestWithdrawRecord()->created_at));
			}
		}
		return $freeWithdrawQuota;
	}

	public function preFreeWithdrawQuotaAmount($coins)
	{
		if(big_decimal($coins)->isGreaterThanOrEqualTo($this->freeWithdrawQuota())){
			$fee = big_decimal(0);
		}else{
			$fee = $this->freeWithdrawQuota()->minus($coins);
		}
		return $fee;
	}

	public function returnFreeWithdrawQuota($coins)
	{
		if(big_decimal($coins)->isGreaterThanOrEqualTo($this->freeWithdrawQuota())){
			$fee = $this->freeWithdrawQuota();
		}else{
			$fee = $coins;
		}
		return $fee;
	}
}
